/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.classinfo.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.classinfo.entity.ClassInfo;
import com.thinkgem.jeesite.modules.classinfo.service.ClassInfoService;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 班级管理Controller
 * @author lihuan
 * @version 2018-12-19
 */
@Controller
@RequestMapping(value = "${adminPath}/classinfo/classInfo")
public class ClassInfoController extends BaseController {

	@Autowired
	private ClassInfoService classInfoService;
	
	@Autowired
	private OfficeService officeService;
	
	@ModelAttribute
	public ClassInfo get(@RequestParam(required=false) String id) {
		ClassInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = classInfoService.get(id);
		}
		if (entity == null){
			entity = new ClassInfo();
		}
		return entity;
	}
	
	@RequiresPermissions("classinfo:classInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(ClassInfo classInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ClassInfo> page = classInfoService.findPage(new Page<ClassInfo>(request, response), classInfo); 
		model.addAttribute("page", page);
		return "modules/classinfo/classInfoList";
	}

	@RequiresPermissions("classinfo:classInfo:view")
	@RequestMapping(value = "form")
	public String form(ClassInfo classInfo, Model model) {
		model.addAttribute("classInfo", classInfo);
		return "modules/classinfo/classInfoForm";
	}

	@RequiresPermissions("classinfo:classInfo:edit")
	@RequestMapping(value = "save")
	public String save(ClassInfo classInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, classInfo)){
			return form(classInfo, model);
		}
		classInfoService.save(classInfo);
		addMessage(redirectAttributes, "保存班级管理成功");
		return "redirect:"+Global.getAdminPath()+"/classinfo/classInfo/";
	}
	
	@RequiresPermissions("classinfo:classInfo:edit")
	@RequestMapping(value = "delete")
	public String delete(ClassInfo classInfo, RedirectAttributes redirectAttributes) {
		classInfoService.delete(classInfo);
		addMessage(redirectAttributes, "删除班级管理成功");
		return "redirect:"+Global.getAdminPath()+"/classinfo/classInfo/?repage";
	}
	
	/**
	 * @author lihuan
	 * @date 2018年11月28日 下午3:44:39
	 * @description 查询班级树形列表
	 */
	 @RequestMapping(value = "classTreeData")
	 @ResponseBody
	 public List<Map<String, Object>> schTreeData(ClassInfo classInfo){
		 Office off = officeService.get(UserUtils.getUser().getOffice().getId());
		 Map<String, Object> oMap = new HashMap<>();
		 oMap.put("id", "");
		 oMap.put("pId", "0");
		 oMap.put("name", "班级");
		 oMap.put("code", "");
		 List<Map<String, Object>> mapList = new ArrayList<>();
		 List<ClassInfo> list = classInfoService.findList(classInfo);
		 for(int i=0;i<list.size();i++){
			 ClassInfo caInfo = list.get(i);
			 if(off.getCode().equals(caInfo.getClassCode())){
				 continue;
			 }
			 Map<String, Object> map = Maps.newHashMap();
			 map.put("id", caInfo.getId());
			 map.put("pId", caInfo.getParentId());
			 map.put("name", caInfo.getClassName());
			 map.put("code", caInfo.getClassCode());
			 mapList.add(map);
		 }
		 mapList.add(oMap);
		return mapList;
	 }

}